package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Model.Lernbgrp;

public class LernbgrpDAO {
	
	public Lernbgrp getLernbgrp(int id)
	{
		Lernbgrp l=null;
		Connection conn=ConnectionProvider.getConnection();
		PreparedStatement statement=null;
		ResultSet rs=null;
		String sql="select * from lernbuerogruppe where lbgruppeID=?";
        try {
			statement = conn.prepareStatement(sql);
			statement.setInt(1, id);
			rs=statement.executeQuery();
			
			if (rs.next())
			{
				l=new Lernbgrp();
				l.setId(id);
				l.setLernbgbez(rs.getString("lernbgbez"));
				
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				rs.close();
				statement.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return l;
	}
	
	public void update(Lernbgrp l)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="update lernbuerogruppe set lernbgbez=? where lbgruppeID=?";
        try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setString(1, l.getLernbgbez());
			statement.setInt(2, l.getId());

			statement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void anlegen(Lernbgrp l)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="insert into lernbuerogruppe VALUES (?, ?)";
        try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, l.getId());
			statement.setString(2, l.getLernbgbez());

			statement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void delete(int id)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="delete from lernbuerogruppe where lbgruppeID= ?";
        try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, id);
			statement.executeUpdate();
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
        finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
